<template>
  <div>
    <h2>注册页面</h2>
    <div>
      <el-container>
        <el-card class="box-card" style="width:400px">
          <el-form>
            <el-form-item  label-width="200" label="用户名">
              <el-input v-model="userForm.username"></el-input>
            </el-form-item>
            <el-form-item  label-width="200" label="密码">
              <el-input v-model="userForm.password"></el-input>
            </el-form-item>
            <el-form-item  label-width="200" >
              <el-button @click="register">注册</el-button>
              <el-button @click="toLogin">返回</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-container>
    </div>
  </div>
</template>

<script>
export default {
  name: 'RegView',
  data () {
    return {
      userForm: {
        name: '',
        enabled: 1,
        username: '',
        password: ''
      }
    }
  },
  methods: {
    // 注册
    register () {
      this.userForm.name = this.userForm.username
      this.axios.post('/user/register', this.userForm)
        .then(res => {
          if (res.data.code === 200) {
            this.$message.success('注册成功')
            this.$router.push('/login')
          } else {
            this.$message.error('注册失败')
            // 刷新页面
            location.reload()
          }
        })
    },
    // 返回登录页面
    toLogin () {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>

</style>
